package eecs285.proj4.clientWin;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.Border;

public class ChannelPanel extends JPanel
{
  private JTextArea chatArea;
  private JTextArea inputArea;
  private JButton postButton;
  //private JButton 
  private JList listenerList;
  private DefaultListModel listenerListModel;
  private JLabel tagLabel;
  protected JPanel rightPanel;
  //private ChannelFrameActionListener listener;
  
  ChannelPanel()
  {
    super();
    //listener = new ChannelFrameActionListener();
    
    //Set the the tag label and its border
    tagLabel = new JLabel("Tag infomation", SwingConstants.CENTER);
    tagLabel.setPreferredSize(new Dimension(200, 120));
    Border raisedbevel = BorderFactory.createRaisedBevelBorder();
    Border loweredbevel = BorderFactory.createLoweredBevelBorder();
    Border compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel);
    tagLabel.setBorder(compound);
    
    //Set the listener list
    listenerListModel = new DefaultListModel();
    listenerList = new JList(listenerListModel);
    freshListenerList();
    JScrollPane listView = new JScrollPane(listenerList);
    listView.setPreferredSize(new Dimension(200, 480));
    
    rightPanel = new JPanel(new BorderLayout());
    rightPanel.add(tagLabel, BorderLayout.NORTH);
    rightPanel.add(listView, BorderLayout.CENTER);
    rightPanel.setPreferredSize(new Dimension(200, 650));
    
    //Set the name and description of the channel
    JLabel name = new JLabel("Channel Name");
    name.setMinimumSize(new Dimension(500,70));
    JPanel namePanel = new JPanel(new FlowLayout());
    namePanel.add(name);
    namePanel.setBorder(compound);
    
    //Set the chat text area
    chatArea = new JTextArea(15, 30);
    chatArea.setFont(new Font("Serif", Font.PLAIN, 16));
    chatArea.setLineWrap(true);
    chatArea.setWrapStyleWord(true);
    freshChatArea();
    JScrollPane chatPane = new JScrollPane(chatArea); 
    chatArea.setEditable(false);
    
    //Set the input area
    inputArea = new JTextArea(2, 30);
    inputArea.setFont(new Font("Serif", Font.PLAIN, 16));
    inputArea.setLineWrap(true);
    inputArea.setWrapStyleWord(true);
    inputArea.setEditable(true);
    postButton = new JButton("post");
    
    JPanel inputPanel = new JPanel(new BorderLayout());
    JPanel inputButtonPanel = new JPanel(new FlowLayout());
    inputButtonPanel.add(postButton, SwingConstants.CENTER);
    inputPanel.add(inputArea, BorderLayout.CENTER);
    inputPanel.add(inputButtonPanel, BorderLayout.SOUTH);
    
    JPanel middlePanel = new JPanel();
    middlePanel.setLayout(new BoxLayout(middlePanel, BoxLayout.Y_AXIS));
    middlePanel.add(namePanel);
    middlePanel.add(chatPane);
    middlePanel.add(inputPanel);
    
    
    setLayout(new BorderLayout());
    add(middlePanel, BorderLayout.CENTER);
    add(rightPanel, BorderLayout.EAST);
  }
  
  private class ChannelFrameActionListener implements ActionListener
  {
    public void actionPerformed(ActionEvent event)
    {
      if(event.getSource().equals(postButton))
      {
        System.out.println("post chat message");
      }
    }
  }
  
  private void freshChatArea()
  {
    System.out.println("fresh chat area");
    chatArea.append("xxx: ahaha\n");
  }
  
  private void freshListenerList()
  {
    listenerListModel.addElement("  ID         NICKNAME");
    System.out.println("freshListenerList");
  }
}